package demo4.mapper;


import demo4.pojo.UserInfo;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


/**
 * Mapper是数据持久层的类，作用是和数据库进行操作，提供基础的增删改查等相关操作。
 * <p>
 * <p>
 * UserMapper该类就是和数据库中一个名为User的表进行操作的
 */
@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    public int insert(UserInfo userInfo) {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "INSERT INTO userinfo(username,password,nickname,age)" +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,userInfo.getUsername());
            ps.setString(2,userInfo.getPassword());
            ps.setString(3,userInfo.getNickname());
            ps.setInt(4,userInfo.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public UserInfo selectByUsername(String username){
        try (Connection connection = dbUtil.getConnection();){
            String sql = "SELECT id,username,password,nickname,age "+
                         "FROM userinfo " +
                         "WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                return userInfo;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<UserInfo> selectAll() {
        List<UserInfo> userList = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection();){
            String sql = "SELECT id,username,password,nickname,age "+
                         "FROM userInfo";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()){
                //每次循环都创建一个新的对象
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                userList.add(userInfo);
            }
            } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return userList;
    }
}







